/**
 * @filename:SwitchOrderInfoController 2020年06月11日
 * @project hotel-switch  V1.0
 * Copyright(c) 2020 Zhang·JZ Co. Ltd.
 * All right reserved.
 */
package hotel.switchs.oversea.biz.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import hotel.base.oversea.constant.CommonConstants;
import hotel.base.oversea.constant.CommonUrlConstants;
import hotel.base.oversea.utils.HttpClientBaseAuth;
import hotel.base.oversea.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.Query;
import java.lang.management.ManagementFactory;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 *@Description: 携程订单控制类
 *@Author: Zhang·jz
 *@date: 2020/6/8
 */
@Slf4j
@RestController
@RequestMapping("/switchTest")
@Api(value = "SwitchTestController",tags = {"获取携程订单信息接口"})
public class SwitchTestController {

    @Autowired
    private RestTemplate restTemplate;

    /**
     * getSwitchsTest
     * @return
     */
    @PostMapping("/getSwitchsTest")
    @ApiOperation(value = "发单接口 将订单发送给相关酒店供应商 ",notes="必须经过auth授权")
    public Result<Object> getSwitchsTest(@RequestBody JSONObject jsonObject) {
        log.info("jsonObject: {}",jsonObject);
        MBeanServer beanServer = ManagementFactory.getPlatformMBeanServer();
        Set<ObjectName> objectNames = null;
        try {
            objectNames = beanServer.queryNames(new ObjectName("*:type=Connector,*"),
                    Query.match(Query.attr("protocol"), Query.value("HTTP/1.1")));
        } catch (MalformedObjectNameException e) {
            e.printStackTrace();
        }
        String port = objectNames.iterator().next().getKeyProperty("port");

        log.info("当前调用端口为{}的携程服务",port);

        log.info("---> 开始调用:美团服务；");
        String accessToken = HttpClientBaseAuth.getAuthToken();
        String host = CommonUrlConstants.MEITUAN_HOST;
        String updateUrl = host + "/mtTest/getMtTest";// +"?access_token="+accessToken

        Map params = new HashMap();
        params.put("retMsg"," 调用 美团测试接口通知 ");
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.valueOf("application/json;UTF-8"));
        HttpEntity<String> strEntity = new HttpEntity<String>(JSON.toJSONString(params),headers);
        //发起请求
        Result response = null;
        try{
            response = restTemplate.postForObject(updateUrl,strEntity,Result.class);
//           response= restTemplate.postForEntity(updateUrl,strEntity,Result.class);
        }catch (Exception e){
            log.error("orderCallback 美团异步通知 请求内部处理接口 requestCheckOneOrderMtNotice 发起请求 异常 catch: {},{},{}",
                    updateUrl, JSON.toJSONString(params), JSON.toJSONString(response));
            log.error(e.getMessage(),e);
        }

        log.info("---> 结束调用:美团服务；");

        return new Result<>(CommonConstants.SUCCESS);
    }

    /**
     *getSwitchsTestFinal
     * @return
     */
    @PostMapping("/getSwitchsTestFinal")
    @ApiOperation(value = "发单接口 将订单发送给相关酒店供应商 ",notes="必须经过auth授权")
    public Result<Object> getSwitchsTestFinal(@RequestBody JSONObject jsonObject) {
        log.info("jsonObject: {}",jsonObject);
        MBeanServer beanServer = ManagementFactory.getPlatformMBeanServer();
        Set<ObjectName> objectNames = null;
        try {
            objectNames = beanServer.queryNames(new ObjectName("*:type=Connector,*"),
                    Query.match(Query.attr("protocol"), Query.value("HTTP/1.1")));
        } catch (MalformedObjectNameException e) {
            e.printStackTrace();
        }
        String port = objectNames.iterator().next().getKeyProperty("port");

        log.info("当前调用端口为{}的携程服务",port);


        log.info("---> 结束调用 getSwitchsTestFinal；");

        return new Result<>(CommonConstants.SUCCESS);
    }


    /**
     * getSwitchsTest
     * @return
     */
    @PostMapping("/getSwitchsTestByModule")
    @ApiOperation(value = "发单接口 将订单发送给相关酒店供应商 ",notes="必须经过auth授权")
    public Result<Object> getSwitchsTestByModule(@RequestBody JSONObject jsonObject) {
        log.info("jsonObject: {}",jsonObject);
        MBeanServer beanServer = ManagementFactory.getPlatformMBeanServer();
        Set<ObjectName> objectNames = null;
        try {
            objectNames = beanServer.queryNames(new ObjectName("*:type=Connector,*"),
                    Query.match(Query.attr("protocol"), Query.value("HTTP/1.1")));
        } catch (MalformedObjectNameException e) {
            e.printStackTrace();
        }
        String port = objectNames.iterator().next().getKeyProperty("port");

        log.info("当前调用端口为{}的携程服务",port);

        log.info("---> 开始调用:美团服务；");
        String accessToken = HttpClientBaseAuth.getAuthToken();
//        String host = CommonUrlConstants.MEITUAN_HOST;
        String host = "http://HOTEL-GATEWAY/api/meituan";
        String updateUrl = host + "/mtTest/getMtTest";// +"?access_token="+accessToken

        Map params = new HashMap();
        params.put("retMsg"," 调用 美团测试接口通知 ");
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.valueOf("application/json;UTF-8"));
        HttpEntity<String> strEntity = new HttpEntity<String>(JSON.toJSONString(params),headers);
        //发起请求
        Result response = null;
        try{
            response = restTemplate.postForObject(updateUrl,strEntity,Result.class);
//           response= restTemplate.postForEntity(updateUrl,strEntity,Result.class);
        }catch (Exception e){
            log.error("orderCallback 美团异步通知 请求内部处理接口 requestCheckOneOrderMtNotice 发起请求 异常 catch: {},{},{}",
                    updateUrl, JSON.toJSONString(params), JSON.toJSONString(response));
            log.error(e.getMessage(),e);
        }

        log.info("---> 结束调用:美团服务；");

        return new Result<>(CommonConstants.SUCCESS);
    }




}
